package yz.cp.back.login.open;

import cn.hutool.crypto.asymmetric.RSA;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import yz.cp.back.common.res.ApiResult;

/**
 * @author Exrickx
 */
@Slf4j
@RestController
@RequestMapping("/web/open")
@Transactional
public class SecurityController {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @RequestMapping(value = "/login",method = RequestMethod.GET)
    public ApiResult needLogin(){

        return ApiResult.Custom().failure(401, "您还未登录");
    }

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ApiResult successLogin(){
        return ApiResult.Custom().success("");
    }

    @RequestMapping(value = "/rsa",method = RequestMethod.GET)
    public ApiResult rsa(){
        RSA rsa=new RSA();
        String privateKey=rsa.getPrivateKeyBase64();
        String publicKey=rsa.getPublicKeyBase64();
        String md5=new Digester(DigestAlgorithm.MD5).digestHex(privateKey);
        redisTemplate.opsForValue().set("rsa:"+md5,privateKey, 180);
        return ApiResult.Custom().put("md5",md5)
            .put("publicKey",publicKey);
}
}
